package com.bolt.report.business.modules.dataset.controller;

import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.report.business.modules.dataset.dto.ReportDataSetDTO;
import com.bolt.report.business.modules.dataset.dto.ReportDataSetQueryCriteria;
import com.bolt.report.business.modules.dataset.dto.converter.ReportDataSetConverter;
import com.bolt.report.business.modules.dataset.entity.ReportDataSetEntity;
import com.bolt.report.business.modules.dataset.service.ReportDataSetService;
import com.bolt.report.business.modules.datasource.dto.DataSetAnalyzeParamDTO;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-11-17 14:15:01
 **/
@RestController
@RequestMapping("report/design/dataset")
@RequiredArgsConstructor
@Api(tags = "")
public class ReportDataSetController {

    private final ReportDataSetService reportDbService;

    /**
     * 列表分页
     *
     * @return
     */
    @PostMapping(value = "/page")
    @ApiOperation(notes = "列表分页列表分页", httpMethod = "GET", value = "列表分页")
    public ResultMessage list(Pageable pageable, ReportDataSetQueryCriteria queryCriteria) {
        PageInfo<ReportDataSetEntity> pageInfo = reportDbService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
        return Results.success(pageInfo.map(K -> ReportDataSetConverter.toDTO(K)));
    }

    /**
     * 根据ID操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询", httpMethod = "GET", value = "根据ID查询")
    public ResultMessage<ReportDataSetDTO> get(@PathVariable String id) {
        ReportDataSetEntity reportDbEntity = reportDbService.findOne(id).get();
        return Results.success(ReportDataSetConverter.toDTO(reportDbEntity));
    }

    /**
     * 新增操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "新增", httpMethod = "POST", value = "新增")
    public ResultMessage<Void> create(@Validated @RequestBody ReportDataSetDTO dao) {
        reportDbService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PutMapping
    @ApiOperation(notes = "编辑", httpMethod = "PUT", value = "编辑")
    public ResultMessage<Void> update(@Validated(ReportDataSetDTO.Update.class) @RequestBody ReportDataSetDTO dao) {
        reportDbService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除", httpMethod = "DELETE", value = "根据ID删除")
    public ResultMessage<Void> doDelete(@RequestBody Set<String> ids) {
        for (String id : ids) {
            reportDbService.deleteById(id);
        }
        return Results.success();
    }


    @GetMapping("/validateCode/{code}")
    @ApiOperation(notes = "根据ID查询", httpMethod = "GET", value = "根据ID查询")
    public ResultMessage validateCode(@PathVariable String code) {
        boolean exist = reportDbService.validateCode(code);
        return Results.success(exist);
    }

    @PostMapping("/analyze")
    @ApiOperation(notes = "解析Spring数据源", httpMethod = "POST", value = "解析Spring数据源")
    public ResultMessage<List<Map<String, Object>>> analyzeDataSet(@RequestBody DataSetAnalyzeParamDTO dto) {
        List<Map<String, Object>> fieldInfo = reportDbService.analyze(dto);
        return Results.success(fieldInfo);
    }

    @GetMapping("/loadDataSet/{type}")
    @ApiOperation(notes = "根据类型查询数据集列表", httpMethod = "GET", value = "根据类型查询数据集列表")
    public ResultMessage loadDataSet(@PathVariable Integer type) {
        List<ReportDataSetEntity> dataSetEntities = reportDbService.findAll(k -> k.lambda().eq(ReportDataSetEntity::getDbType, type).build());
        return Results.success(ReportDataSetConverter.toDTO(dataSetEntities));
    }
}